package me.zixing.timer.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

/**
 * Created by bingh on 2017/10/21.
 *
 * 时间计算工具
 */
public class TimeComputUtil {

    private static String path = System.getProperty("user.home");
    private static Properties properties = new Properties();
    private static SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    private static long endTime;
    public volatile static boolean stopComput;
    public volatile static boolean start = true; //启动计时模式
    public volatile static boolean reset = true; //重置模式
    public volatile static boolean set = true; //设置模式
    public volatile static boolean normal = true;//正常模式

    static{
        try{
            File prop = new File(path + "/time.properties");
            if(!prop.exists()){
                prop.createNewFile();
                if(prop.exists()){
                    FileOutputStream outputStream = new FileOutputStream(prop);
                    outputStream.write("timeLong=0".getBytes());
                    outputStream.flush();
                    outputStream.close();
                }
            }

            properties.load(new FileInputStream(prop));
            format.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String getTime(){
        long timeAsLong = getTimeAsLong();
        String timeAsString = format.format(new Date(timeAsLong));
        return timeAsString;
    }

    private static long getTimeAsLong(){
        try{
            properties.load(new FileInputStream(path+"/time.properties"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return Long.parseLong(properties.getProperty("timeLong"));
    }

    public static void setTime(long time){
        properties.setProperty("timeLong",String.valueOf(time));
    }

    public static String[] computTime(){

        if(reset && !set && !start && !normal){
            return getTime().split(":");
        }

        if(normal && !set && !start && !reset){
            return getTime().split(":");
        }

        if(set && !normal && !start && !reset){
            TimeSetUtil.list.set(TimeSetUtil.index,"_");
            String[] times = new String[]{
                    TimeSetUtil.list.get(0)+""+TimeSetUtil.list.get(1),
                    TimeSetUtil.list.get(2)+""+TimeSetUtil.list.get(3),
                    TimeSetUtil.list.get(4)+""+TimeSetUtil.list.get(5)

            };
            return times;
        }

        //计时模式
        if (start && !set && !reset && !normal){
            long time = computTimeAsLong();
            if(time <= 0){
                start = false;
                return "00:00:00".split(":");
            }else{
                return format.format(new Date(time)).split(":");
            }
        }

        return "00:00:00".split(":");
    }


    public static synchronized void normalOption(){
        normal = true;
        set = false;
        reset = false;
        start = false;
    }
    public static synchronized void setOption(){
        set = true;
        reset = false;
        start = false;
        normal = false;
    }
    public static synchronized void resetOption(){
        reset = true;
        set = false;
        start = false;
        normal = false;
    }
    public static synchronized void startOption(){
        start = true;
        set = false;
        reset = false;
        normal = false;
    }


    public static long computTimeAsLong(){
        long time = endTime - System.currentTimeMillis();
        return time;
    }

    public static void computEndTime(){
        long time = getTimeAsLong();
        endTime = System.currentTimeMillis() + time;
    }
}
